package com.jydd.liaomingframe.lrucache;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hzh.easy.cache.CacheManager;
import com.hzh.easy.cache.base.CacheFactory;
import com.hzh.fast.permission.FastPermission;
import com.hzh.fast.permission.callback.PermissionCallback;
import com.jydd.liaomingframe.R;
import com.jydd.liaomingframe.app.SpecialApplication;
import com.lm.liaoming.tool.Utils;
import com.lm.liaoming.utils.AsyncExecutor;
import com.lm.liaoming.utils.SoftKeyboardUtil;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

/**
 * created by 韦敏敏
 * on 2019/11/26
 */
public class EasyCacheActivity extends AppCompatActivity {
    private static final String TAG = EasyCacheActivity.class.getSimpleName();
    private int focusHashCode;
    private EditText nameInput;
    private EditText signInput;
    private Button saveCache;
    private Button readCache;
    private ProgressBar progressBar;
    private TextView result;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_easycache);
        nameInput = findViewById(R.id.name);
        signInput = findViewById(R.id.sign);
        saveCache = findViewById(R.id.saveCache);
        readCache = findViewById(R.id.readCache);
        progressBar = findViewById(R.id.progress);
        result = findViewById(R.id.result);
        CacheManager.getInstance().init(getApplicationContext());
        // 请求存储权限
        String[] perms;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
            perms = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        } else {
            perms = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
        }
        requestPermissions(perms, new Runnable() {
            @Override
            public void run() {
                Utils.i("授权成功, 可进行下一步操作");
            }
        });
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.saveCache:
                        save();
                        break;
                    case R.id.readCache:
                        read();
                        break;
                    default:
                        break;

                }

            }
        };
        saveCache.setOnClickListener(listener);
        readCache.setOnClickListener(listener);
    }

    /**
     * 保存用户信息保存到到缓存中
     */
    private void save(){
        boolean isComplete = checkInput();
        if(!isComplete)
            return;
        final String name = nameInput.getText().toString().trim();
        final String sign = signInput.getText().toString().trim();
        AsyncExecutor.getInstance().execute(getActivity(), new AsyncExecutor.AsyncCallback<Boolean>(){
            @Override
            protected void runBefore() {
                super.runBefore();
                showProgress();
            }
            @Override
            protected Boolean running() {
                boolean isSuccess;
                try {
                    UserInfoCache infoCache = CacheFactory.create(UserInfoCache.class);
                    UserInfoCacheParam params = new UserInfoCacheParam();
                    params.putUserId(name);
                    UserInfo userInfo = new UserInfo(name, sign);
                    infoCache.put(params, userInfo);
                    isSuccess = true;
                } catch (Exception e) {
                    e.printStackTrace();
                    isSuccess = false;
                }
                return isSuccess;
            }

            @Override
            protected void runAfter(Boolean isSuccess) {
                hideProgress();
                if(isSuccess){
                    toastMsg("保存成功!");
                } else {
                    toastMsg("保存失败!");
                }
            }
        });


    }

    /**
     * 从缓存中读取用户信息
     */
    private void read(){
        if(nameInput.isFocused()){
            focusHashCode = nameInput.hashCode();
        }
        if(signInput.isFocused()){
            focusHashCode = signInput.hashCode();
        }
        final EditInputDialog dialog = new EditInputDialog(getActivity());
        dialog.setOnClickListener(new EditInputDialog.OnClickListener() {
            @Override
            public void onClickConfirm() {
                final String name = dialog.getInput();
                if(TextUtils.isEmpty(name)){
                    toastMsg("姓名不能为空");
                    return;
                }
                AsyncExecutor.getInstance().execute(new AsyncExecutor.AsyncCallback<UserInfo>() {
                    @Override
                    protected void runBefore() {
                        super.runBefore();
                        dialog.dismiss();
                        showProgress();
                    }

                    @Override
                    protected UserInfo running() {
                        UserInfoCache infoCache = CacheFactory.create(UserInfoCache.class);
                        UserInfoCacheParam params = new UserInfoCacheParam().putUserId(name);
                        return infoCache.get(params);
                    }

                    @Override
                    protected void runAfter(UserInfo userInfo) {
                        hideProgress();
                        toastMsg("查询成功,请查看结果!");
                        if(null == userInfo){
                            result.setText("无该用户信息，请确认后再试!");
                        } else {
                            result.setText("查询到用户: " + "name : "
                                    + userInfo.getName() + " sign: " + userInfo.getSign());
                        }
                    }
                });


            }
            @Override
            public void onClickCancel() {
                    dialog.dismiss();
            }
        });
        dialog.setOnDisplayChangeListener(new EditInputDialog.OnDisplayChangeListener() {
            @Override
            public void onDisplayChange(boolean isVisible) {
                if(isVisible){
                    setInputFocus(false);
                    SoftKeyboardUtil.hideSoftKeyboard(getActivity(), getWindow().getDecorView());
                    SpecialApplication.getInstance().post(new Runnable() {
                        @Override
                        public void run() {
                            SoftKeyboardUtil.showSoftKeyboard(getActivity(), dialog.getInputEditText());
                        }
                    }, 400);

                } else {
                    setInputFocus(true);
                    SpecialApplication.getInstance().post(new Runnable() {
                        @Override
                        public void run() {
                            if(focusHashCode == nameInput.hashCode()){
                                nameInput.requestFocus();
                            }
                            if(focusHashCode == signInput.hashCode()){
                                signInput.requestFocus();
                            }
                        }
                    }, 100);
                }
            }
        });
        dialog.show();
    }
    /**
     * 检查用户是否将姓名和签名输入
     * @return
     */
    private boolean checkInput(){
        String name = nameInput.getText().toString().trim();
        String sign = signInput.getText().toString().trim();
        if(TextUtils.isEmpty(name)){
            toastMsg("请输入姓名");
            return false;
        }
        if(TextUtils.isEmpty(sign)){
            toastMsg("请输入个性签名");
            return false;
        }
        return true;
    }
    private void requestPermissions(String[] perms, final Runnable grantedAfterRun){
        FastPermission.getInstance().request(getActivity(), new PermissionCallback() {
            @Override
            public void onGranted() {
                if(grantedAfterRun != null)
                    grantedAfterRun.run();
            }

            @Override
            public void onDenied(List<String> perms) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("申请权限")
                        .setMessage("请允许app申请的所有权限,以便正常使用")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                // 再次请求未允许的权限
                                String[] againPerms = new String[perms.size()];
                                for(int j=0; j<perms.size(); j++){
                                    againPerms[j] = perms.get(j);
                                }
                                requestPermissions(againPerms,grantedAfterRun);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).create().show();
            }
        }, perms);
    }
    private FragmentActivity getActivity(){
        return this;
    }
    private void toastMsg(String msg){
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
    private void showProgress(){
        if(progressBar == null){
            progressBar = findViewById(R.id.progress);
        }
        progressBar.setVisibility(View.VISIBLE);
    }
    private void hideProgress(){
        if(progressBar == null){
            progressBar = findViewById(R.id.progress);
        }
        progressBar.setVisibility(View.GONE);
    }

    /**
     * 设置界面EditText的焦点
     * @param isVisible
     */
    private void setInputFocus(boolean isVisible){
        nameInput.setFocusable(isVisible);
        nameInput.setFocusableInTouchMode(isVisible);
        signInput.setFocusable(isVisible);
        signInput.setFocusableInTouchMode(isVisible);
    }
}
